package com.zeedev.flyingsaucer.beers.activities.umbrella;

import android.content.Context;
import android.widget.Toast;
import com.google.inject.Inject;
import mobi.zeedev.flyingsaucer.common.WebLinkModule.interfaces.UfoClubClient;
import roboguice.util.Ln;
import roboguice.util.RoboAsyncTask;

/**
 * User: James
 * Date: 2/17/11
 * Time: 1:31 PM
 */
class UmbrellaDownloader extends RoboAsyncTask {

    final UmbrellaActivity mActivity;

    @Inject
    UfoClubClient client;

    @Inject
    UmbrellaDownloader(Context mContext) {
        super();
        this.mActivity = (UmbrellaActivity) mContext;
    }

    @Override
    public Object call() throws Exception {
        client.login();
        return null;
    }

    @Override
    protected void onPreExecute() throws Exception {
        super.onPreExecute();


        mActivity.setProgressBarIndeterminateVisibility(true);
    }

    @Override
    protected void onSuccess(Object o) throws Exception {
        super.onSuccess(o);

        if (client.isLoggedIn()) { // && loginTest) {
            Ln.i("Client is logged in.");
            mActivity.viewHome();
        } else {
            Ln.i("Client is not logged in.");
//                viewLogin();
            throw new Exception("Client not logged in.");
        }
    }

    @Override
    protected void onInterrupted(InterruptedException e) {
        super.onInterrupted(e);

        //TODO fill this in with destory listener thingy

    }

    @Override
    protected void onFinally() throws RuntimeException {
        super.onFinally();


        mActivity.setProgressBarIndeterminateVisibility(false);
    }

    @Override
    protected void onException(Exception e) throws RuntimeException {
        super.onException(e);

        Ln.e(e, "There was an error logging in.");
        Toast.makeText(mActivity, e.getMessage(), Toast.LENGTH_SHORT).show();

        mActivity.viewLogin();
    }
}
